package cn.ygh.bbo.common.utils;

import cn.ygh.bbo.common.exception.TipException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author ygh
 * @Desc 通用文件工具
 * @date 2019/10/1
 */
public class FileUtils {
    /**
     * 文件上传的文件夹格式
     */
    public static final String FILE_UPLOAD_PATH = "/files/{0}";

    /**
     * 文件上传excel的路径
     */
    public static final String FILE_UPLOAD_PATH_EXCEL = "/files/excel";

    /**
     * @param path     硬盘路径
     * @param filename 文件名
     * @param data     文件数据
     * @return 图片的url地址
     */
    public static String fileUploadSave(String path, String filename, byte[] data) {
        String ext = getFileExt(filename);
        //生成随机文件名 防止文件名冲突
        filename = UUID.randomUUID().toString() + (StringUtils.isBlank(ext) ? "" : "." + ext);
        //    /files/2019
        String sdest = MessageFormat.format(FILE_UPLOAD_PATH, new SimpleDateFormat("yyyyMMdd").format(new Date())) + "/" + filename;
        //硬盘路径
        String hdDest = path+sdest;
        File dest = new File(hdDest);
        //目标目录的父目录，不存在则创建
        if (!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        FileOutputStream fos = null;
        //开始写硬盘
        try {
            fos = new FileOutputStream(dest);
            IOUtils.write(data, fos);
        } catch (Exception e) {
            throw new TipException("file error");
        } finally {
            //关闭输出流
            IOUtils.closeQuietly(fos);
        }

        return sdest;
    }

    /**
     * @param path     硬盘路径
     * @param workbook 文件数据
     * @return
     */
    public static String fileExcelSave(String path, Workbook workbook) {
        String ext = ".xls";
        if (workbook instanceof XSSFWorkbook) {
            ext = ".xlsx";
        }
        String filename = UUID.randomUUID().toString() + ext;
        //    /files/excel
        String sdest = FILE_UPLOAD_PATH_EXCEL + "/" + filename;
        //硬盘路径
        String hdDest = path+sdest;
        File dest = new File(hdDest);
        //目标目录的父目录，不存在则创建
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        FileOutputStream fos = null;
        //开始写硬盘
        try {
            fos = new FileOutputStream(dest);
            workbook.write(fos);
        } catch (Exception e) {
            throw new TipException("excel error");
        } finally {
            IOUtils.closeQuietly(fos);
        }
        return sdest;
    }

    /**
     * 根据链接获取文件名
     *
     * @param url
     * @return
     */
    public static String getFileNameFormUrl(String url) {
        String fileName = "";
        if (StringUtils.isBlank(fileName)) {
            fileName = url.substring(url.lastIndexOf("/") + 1);
        }
        if (StringUtils.isBlank(fileName)) {
            fileName = new Random().nextInt(10000) + System.currentTimeMillis() + "";
        }
        return fileName;
    }

    /**
     * 文件后缀
     *
     * @param fileName
     * @return
     */
    public static String getFileExt(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
}
